package cn.rkylin.oms.base.mdm.controller;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.base.brand.service.BrandServiceImpl;
import cn.rkylin.oms.base.channel.service.IChannelService;
import cn.rkylin.oms.base.mdm.vo.ReceiveMdmVO;
import cn.rkylin.oms.base.mdm.vo.ResponseVO;
import cn.rkylin.oms.base.mdm.vo.ResultVO;
import cn.rkylin.oms.goods.service.IGoodsCategoryService;
import cn.rkylin.oms.goods.service.IGoodsService;
import cn.rkylin.oms.system.project.service.ProjectManagerService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018-1-24.
 */
@Controller
@RequestMapping("/mdmManager")
public class MdmManagerController extends ApolloController {

    @Autowired
    public ProjectManagerService projectManagerService;

    @Autowired
    public BrandServiceImpl brandService;
    
    @Autowired
    public IChannelService channelService;

    @Autowired
    private IGoodsCategoryService goodsCategoryService;

    /**
     * 系统商品服务层
     */
    @Autowired
    private IGoodsService goodsService;

    private static final Log logger = LogFactory.getLog(MdmManagerController.class);

    /**
     * 开放结构，用来监听主数据是否有变化
     *
     * @param content
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/receiveMdm", method = RequestMethod.POST,produces="text/html; charset=UTF-8")
    public String receiveMdm(String content) throws Exception {//@RequestBody ReceiveMdmVO request
    	
    	logger.info(content);
        Map<String, Object> map = JSONObject.parseObject(content.replace("content=",""), new TypeReference<Map<String, Object>>() {
        });

        ResponseVO responseVO =  new ResponseVO();
        ResultVO resultVO = new ResultVO();
        responseVO.setResult(resultVO);
//        ReceiveMdmVO vo = JsonToObject(content.replace("content=",""));
        Object object = null;
        try {
            if ("mdm_settlement_project".equals(map.get("mdmType").toString())) {
                object = projectManagerService.receiveMdm(JSON.toJSONString(map.get("inputData")), map.get("operateType").toString());
                responseVO.setStatus("S1");
                responseVO.setMessage("成功！");
            } else if ("mdm_non_settlement_project".equals(map.get("mdmType").toString())) {
                object = projectManagerService.receiveMdmNon(JSON.toJSONString(map.get("inputData")), map.get("operateType").toString());
                responseVO.setStatus("S1");
                responseVO.setMessage("成功！");
            } else if ("mdm_brand_template_instance".equals(map.get("mdmType").toString())) {
                Map<String,Object> resultMap = brandService.receiveBrand(JSON.toJSONString(map.get("inputData")), map.get("operateType").toString());
                object = resultMap.get("data");
                responseVO.setStatus((String)resultMap.get("status"));
                responseVO.setMessage((String)resultMap.get("message"));
            } else if("mdm_channel_template_instance".equals(map.get("mdmType").toString())) {
                object = channelService.receiveChannel(JSON.toJSONString(map.get("inputData")), map.get("operateType").toString());
                responseVO.setStatus("S1");
                responseVO.setMessage("成功！");
            } else if("mdm_channel_cloud_template_instance".equals(map.get("mdmType").toString())) {
                object = channelService.receiveChannelNew(JSON.toJSONString(map.get("inputData")), map.get("operateType").toString());
                responseVO.setStatus("S1");
                responseVO.setMessage("成功！");
            } else if ("mdm_goods_category_instance".equals(map.get("mdmType").toString())) { //商品分类
                object = goodsCategoryService.receiveGoodsCategory(JSON.toJSONString(map.get("inputData")), map.get("operateType").toString(),"51f17f6ddbae11e68c4d001e67c7ec3a");
                responseVO.setStatus("S1");
                responseVO.setMessage("成功！");
            } else if ("mdm_goods_template_instance".equals(map.get("mdmType").toString())) { //商品
                object = goodsService.receiveGoods(JSON.toJSONString(map.get("inputData")), map.get("operateType").toString(),"51f17f6ddbae11e68c4d001e67c7ec3a");
                responseVO.setStatus("S1");
                responseVO.setMessage("成功！");
            } else {
                responseVO.setStatus("-1");
                responseVO.setMessage("主数据类型不匹配");
            }
            responseVO.getResult().setData(map.get("inputData"));
            responseVO.getResult().setStatus("Y6");
            responseVO.getResult().setMessage("接受到数据并处理成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            responseVO.setStatus("S4");
            responseVO.setMessage(e.getMessage());
            responseVO.getResult().setData(null);
            responseVO.getResult().setStatus("Y7");
            responseVO.getResult().setMessage("处理失败");
        }
        return JSON.toJSONString(responseVO, SerializerFeature.WriteMapNullValue);
    }

    private ReceiveMdmVO JsonToObject(String json) {
        ReceiveMdmVO vo = new ReceiveMdmVO();

        JSONObject JsonObject = JSON.parseObject(json);
        String inputData = JsonObject.getString("inputData");//   getJSONObject("result")
        JsonObject.remove("inputData");

        vo = JSON.parseObject(JsonObject.toJSONString(), ReceiveMdmVO.class);
        vo.setInputData(inputData);

        return vo;
    }
}
